{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#51CTO课程频道：http://edu.51cto.com/lecturer/index/user_id-12330098.html\n",
    "#优酷频道：http://i.youku.com/sdxxqbf\n",
    "#微信公众号：深度学习与神经网络\n",
    "#Github：https://github.com/Qinbf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.02328653  0.63652761 -0.10532355]\n"
     ]
    }
   ],
   "source": [
    "#输入数据\n",
    "X = np.array([[1,3,3],\n",
    "              [1,4,3],\n",
    "              [1,1,1]])\n",
    "#标签\n",
    "Y = np.array([1,1,-1])\n",
    "#权值初始化，1行3列，取值范围-1到1\n",
    "W = (np.random.random(3)-0.5)*2\n",
    "print(W)\n",
    "#学习率设置\n",
    "lr = 0.11\n",
    "#计算迭代次数\n",
    "n = 0\n",
    "#神经网络输出\n",
    "O = 0\n",
    "\n",
    "def update():\n",
    "    global X,Y,W,lr,n\n",
    "    n+=1\n",
    "    O = np.sign(np.dot(X,W.T))\n",
    "    W_C = lr*((Y-O.T).dot(X))/int(X.shape[0])\n",
    "    W = W + W_C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.0500468   0.56319428 -0.17865688]\n",
      "1\n",
      "[-0.12338013  0.48986095 -0.25199022]\n",
      "2\n",
      "[-0.19671347  0.41652761 -0.32532355]\n",
      "3\n",
      "Finished\n",
      "epoch: 3\n",
      "k= 1.28034879596\n",
      "d= -0.604670233464\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw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XCOEGoBr4KTA7hPDFbFhDjNEjuzMQQrgY+AvwDtCOdER6F6BbnnWVq8Xr00vtgQkhzAZm\nxhjr86mq/IUQrgBGkJYGNgLOAxYANcv7GkURBmKMwxffZnU+aXngZWDfGOOH+VZW1rYHniTtZo+k\nPg8AtwPH5VVUmetJutZPfenxY4E7Cl5NZViP9Hd6Q6ABeAXo5i73gnI2IHvfBe4G1gE+BJ4Bdoox\nzlzeFyiKPgOSJCk/ue8ZkCRJ+TIMSJJU4QwDkiRVOMOAJEkVzjAgSVKFMwxIklThDAOSJFU4w4Ak\nSRXOMCBJUoUzDEiSVOEMA5IkVbj/D1+0CuXwXxLqAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x21ba9f937b8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for _ in range(100):\n",
    "    update()#更新权值\n",
    "    print(W)#打印当前权值\n",
    "    print(n)#打印迭代次数\n",
    "    O = np.sign(np.dot(X,W.T))#计算当前输出\n",
    "    if(O == Y.T).all(): #如果实际输出等于期望输出，模型收敛，循环结束\n",
    "        print('Finished')\n",
    "        print('epoch:',n)\n",
    "        break\n",
    "\n",
    "#正样本\n",
    "x1 = [3,4]\n",
    "y1 = [3,3]\n",
    "#负样本\n",
    "x2 = [1]\n",
    "y2 = [1]\n",
    "\n",
    "#计算分界线的斜率以及截距\n",
    "k = -W[1]/W[2]\n",
    "d = -W[0]/W[2]\n",
    "print('k=',k)\n",
    "print('d=',d)\n",
    "\n",
    "xdata = np.linspace(0,5)\n",
    "\n",
    "plt.figure()\n",
    "plt.plot(xdata,xdata*k+d,'r')\n",
    "plt.plot(x1,y1,'bo')\n",
    "plt.plot(x2,y2,'yo')\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
